#!/bin/bash
#=========================================
# 脚本用于更新,rsync文件到 ds_ip 
#=========================================
#!/bin/bash

temp_path=$(dirname "$0")
cd $temp_path
real_path=$(pwd)
cd $real_path

#================================================================
#echo 函数
#================================================================
echo_function()
{
    color=$1
    statement=$2
    no_wrap=$3
    [[ $no_wrap != 'no' ]] && echo -e "\e[1;"$color" $statement \e[0m" || echo -ne "\e[1;"$color" $statement \e[0m"
}

#echo_function 35m "请输入目标目录{test1,test2,test3,test4}: " "no"
#read SOURCEDIR
#SOURCEDIR=${SOURCEDIR:-'test1'}

#\cp -rf *.sh /data/share/data/yunwei/mwxt/sh/bk/
#========定义变量===========================================================================================
ds_ip=111.11.11.111  # 目标IP

database_password=123456 # 数据库密码
echo "$out_server_dir"
in_php_center_file='/data/web/*'  # 原目录路径
out_php_center_file="/data/web/"  # 目标目录路径

#search other infomation
game_senter_database=`cat /data/web/app.ini | grep database.m1.dsn | head -1 | awk -F= '{print$5}' | awk -F\" '{print$1}'`
game_senter_database_port=`cat /data/web/app.ini | grep database.m1.dsn | head -1 | awk -F= '{print$4}' | awk -F\; '{print$1}'`

new_date=`date +"%Y-%m-%d-%H-%m-%S"`
#备份数据链接 #端口 数据库
mysqldump_link(){
	#端口 数据库
        #导出数据库并带创建库语句
	/usr/local/mysql/bin/mysqldump --host=127.0.0.1 -p${database_password} --port=${1} --no-data -B ${2} > install-${2}.${new_date}.sql
        #只导出数据库表结构
	/usr/local/mysql/bin/mysqldump --host=127.0.0.1 -p${database_password} --port=${1} -d ${2} > install-${2}.${new_date}.sql
        #导出数据库表机构和数据
	/usr/local/mysql/bin/mysqldump --host=127.0.0.1 -p${database_password} --port=${1}  ${2} > ../install-${2}.${new_date}.sql
}
#========定义变量==========================================================================================





road=`echo $real_path | rev | awk -F/ '{print $1}' |rev`
[ "$road" != "sh" ] && echo -e "\e[32m 本脚本不是运行在sh目录下，结束脚本  \e[0m"  && exit 1

#========定义变量===============================================================================开始============
_start_time=`date '+%F %T'`
tar_time=`date +"%Y-%m-%d-%H-%m-%S"`
red_zero=`echo -e "\e[031m0\e[0m"`
my_echo_green() {
echo -e "\e[32m$*\e[0m"
echo "=="
}
my_echo_red() {
echo -e "\e[31m$*\e[0m"
echo "=="
}
log_file="/tmp/${0}.log"
touch "$log_file"
#================================================================
#sshd 连接
#================================================================
sshto(){
	ssh -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o ConnectTimeout=10 -o ServerAliveInterval=60 $@
}

#sshto -p $sshd_port   $service_ip "cd /data/"$service_name"/sh && sh stop_server.sh" >/dev/null 2>&1

#================================================================
#scp 连接
#================================================================
scpto(){
	scp -r -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o ConnectTimeout=10 -o ServerAliveInterval=60 $@
}



#========定义变量===============================================================================结束============

begin_time(){
	#开始执行脚本的时间
	BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"`
	begin_data=`date -d  "$BEGINTIME" +%s`    #把当前时间转化为Linux时间
}

finish_time(){
	ENDTIME=`date +"%Y-%m-%d %H:%M:%S"`
	end_data=`date -d  "$ENDTIME" +%s`
	spend_second=`expr $end_data - $begin_data`  #计算2个时间的差
	spend_minute=`expr $spend_second / 60 `  #计算2个时间的差
	qumo_minute=`expr $spend_second % 60 `  #计算2个时间的差
	#qumo_minute=`expr $spend_second / 60 `  #计算2个时间的差
	
	echo "------------------${_start_time}------------------------------" >> ${log_file}
	echo "使用时间为:${spend_second} 秒 相当于: ${spend_minute} 分钟  ${qumo_minute} 秒" >> ${log_file}

}

#----------------------------------------------------------------------------------------------------------------




#----------------------------------------------------------------------------------------------------------------
#开始服务端文件

#更新后台
rsync_html(){
	cd $real_path
	echo -e "\e[35m ==============================开始更新===========================================\e[0m"
	ssh -p 22 -o StrictHostKeyChecking=no -q root@${ds_ip} "cd ${out_php_center_file} && rm -rf *${center}*.sql "
	mysqldump_link ${center}  ${game_senter_database} 
    rsync -avczp -p  --progress --delete --partial --exclude="*.svn" --exclude="*.log" --exclude="head_images" --exclude="*.xls" --exclude="*.txt"  --exclude="tmp" --exclude="cache"  --exclude="php-session" --exclude="sh" -e "ssh -p 22 -o StrictHostKeyChecking=no -q" ${in_php_center_file}   root@${ds_ip}:${out_php_center_file}
    rsync -avczp -p  --progress --delete --partial --exclude="*.svn"  -e "ssh -p 22 -o StrictHostKeyChecking=no -q" ../*${senter}*.sql    root@${ds_ip}:${out_php_center_file}
    rsync -avczp -p  --progress --delete --partial --exclude="*.svn"  -e "ssh -p 22 -o StrictHostKeyChecking=no -q" /etc/php.ini    root@${ds_ip}:${out_php_center_file}
    rsync -avczp -p  --progress --delete --partial --exclude="*.svn"  -e "ssh -p 22 -o StrictHostKeyChecking=no -q" /usr/local/msn.adm.conf     root@${ds_ip}:${out_php_center_file}
    rm -rf ../*${center}*.sql 
}


#----------------------------------------------------------------------------------------------------------------
end_echo(){
	_end_time=`date '+%F %T'`
	echo 
	echo ==============================
	echo 
	echo "start_time: $_start_time"
	echo "end_time: $_end_time"
	echo 
	echo "更新完成!"
	echo "更新使用时间为:${spend_second} 秒 相当于: ${spend_minute} 分钟 ${qumo_minute} 秒"
}
#
echo "-----------------------------------------"
#read -p "请确认是否执行更新操作(Y/N)? :" y_or_n
#[[ "$y_or_n" != "Y" && "$y_or_n" != "y" ]] && exit 10

#==========================================================
begin_time   
#------------- 

#update     
rsync_html

#------------- 
finish_time                     
#
end_echo                                         #最后输出 
#==========================================================

